package verify.aliyun;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.cloudauth.model.v20180916.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.sys.SysConfigVerifyMemory;
import o2o.dao.PO.sys.SysConfigVerify;

public class RPBasicUtils {
    //创建DefaultAcsClient实例并初始化
    private static DefaultProfile profile = null;    //您的Access Key Secret
    private static IAcsClient client = null;
    private static String biz = "postmanVerify"; //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法：https://help.aliyun.com/document_detail/59975.html

    //系统启动时，初始化
    public static void init() {
        SysConfigVerify configVerify = SysConfigVerifyMemory.getInstance().get(null);
        if (null != configVerify && ValidateUtil.isAbsInteger(configVerify.getIsAble()) && 0 == 1 - configVerify.getIsAble()) {//不为空，且启用时才加载
            profile = DefaultProfile.getProfile(
                    "cn-hangzhou",             //默认
                    configVerify.getAliAccessKeyId(),         //您的Access Key ID
                    configVerify.getAliAccessKeySecret());    //您的Access Key Secret
            profile.getHttpClientConfig().setIgnoreSSLCerts(true);
            client = new DefaultAcsClient(profile);
            biz = configVerify.getAliVerifyBiz(); //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法：https://help.aliyun.com/document_detail/59975.html
        }

    }

    //手机端获取认证token
    public static String getVerifyToken(String ticketId) {
//        //创建DefaultAcsClient实例并初始化
//        DefaultProfile profile = DefaultProfile.getProfile(
//                "cn-hangzhou",             //默认
//                "LTAIQPjOO5t9H9Oy",         //您的Access Key ID
//                "UN8gaJ6nLypQpLqikOfXIFpZfLfY4C");    //您的Access Key Secret
//        profile.getHttpClientConfig().setIgnoreSSLCerts(true);
//        IAcsClient client = new DefaultAcsClient(profile);
//        String biz = "postmanVerify"; //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法：https://help.aliyun.com/document_detail/59975.html
//        int statusCode = -1; //-1 未认证, 0 认证中, 1 认证通过, 2 认证不通过
//        String ticketId = UUID.randomUUID().toString(); //认证ID, 由使用方指定, 发起不同的认证任务需要更换不同的认证ID
//        String token = null; //认证token, 表达一次认证会话
//        int statusCode = -1; //-1 未认证, 0 认证中, 1 认证通过, 2 认证不通过

        if (ValidateUtil.isNull(biz) || null == client) {
            System.out.println("实名认证，系统配置不正确，请联系管理员");
            return null;
        }

        //1. 服务端发起认证请求, 获取到token
        //GetVerifyToken接口文档：https://help.aliyun.com/document_detail/57050.html
        GetVerifyTokenRequest getVerifyTokenRequest = new GetVerifyTokenRequest();
        getVerifyTokenRequest.setBiz(biz);
        getVerifyTokenRequest.setTicketId(ticketId);
        try {
            GetVerifyTokenResponse response = client.getAcsResponse(getVerifyTokenRequest);
            if (null == response || null == response.getData() || null == response.getData().getVerifyToken()) {
                return null;
            }
            return response.getData().getVerifyToken().getToken(); //token默认30分钟时效，每次发起认证时都必须实时获取
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        //2. 服务端将token传递给无线客户端
        //3. 无线客户端用token调起无线认证SDK
        //4. 用户按照由无线认证SDK组织的认证流程页面的指引，提交认证资料
        //5. 认证流程结束退出无线认证SDK，进入客户端回调函数
        //6. 服务端查询认证状态(客户端回调中也会携带认证状态, 但建议以服务端调接口确认的为准)
        //GetStatus接口文档：https://help.aliyun.com/document_detail/57049.html
//        GetStatusRequest getStatusRequest = new GetStatusRequest();
//        getStatusRequest.setBiz(biz);
//        getStatusRequest.setTicketId(ticketId);
//        try {
//            GetStatusResponse response = client.getAcsResponse(getStatusRequest);
//            statusCode = response.getData().getStatusCode();
//        } catch (ServerException e) {
//            e.printStackTrace();
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//        //7. 服务端获取认证资料
//        //GetMaterials接口文档：https://help.aliyun.com/document_detail/57641.html
//        GetMaterialsRequest getMaterialsRequest = new GetMaterialsRequest();
//        getMaterialsRequest.setBiz(biz);
//        getMaterialsRequest.setTicketId(ticketId);
//        if (1 == statusCode || 2 == statusCode) { //认证通过or认证不通过
//            try {
//                GetMaterialsResponse response = client.getAcsResponse(getMaterialsRequest);
//                //后续业务处理
//            } catch (ServerException e) {
//                e.printStackTrace();
//            } catch (ClientException e) {
//                e.printStackTrace();
//            }
//        }

        return null;
    }


    //查询认证状态
    public static int getVerifyStatus(String ticketId) {
        //创建DefaultAcsClient实例并初始化
//        DefaultProfile profile = DefaultProfile.getProfile(
//                "cn-hangzhou",             //默认
//                "LTAIQPjOO5t9H9Oy",         //您的Access Key ID
//                "UN8gaJ6nLypQpLqikOfXIFpZfLfY4C");    //您的Access Key Secret
//        profile.getHttpClientConfig().setIgnoreSSLCerts(true);
//        IAcsClient client = new DefaultAcsClient(profile);
//        String biz = "postmanVerify"; //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法：https://help.aliyun.com/document_detail/59975.html
        int statusCode = -1; //-1 未认证, 0 认证中, 1 认证通过, 2 认证不通过

        if (ValidateUtil.isNull(biz) || null == client) {
            System.out.println("实名认证，系统配置不正确，请联系管理员2");
            return -1;
        }

        //6. 服务端查询认证状态(客户端回调中也会携带认证状态, 但建议以服务端调接口确认的为准)
        //GetStatus接口文档：https://help.aliyun.com/document_detail/57049.html
        GetStatusRequest getStatusRequest = new GetStatusRequest();
        getStatusRequest.setBiz(biz);
        getStatusRequest.setTicketId(ticketId);
        try {
            GetStatusResponse response = client.getAcsResponse(getStatusRequest);
            statusCode = response.getData().getStatusCode();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return statusCode;
    }


    //获取认证资料
    public static GetMaterialsResponse.Data getVerifyInfo(String ticketId) {
        //创建DefaultAcsClient实例并初始化
//        DefaultProfile profile = DefaultProfile.getProfile(
//                "cn-hangzhou",             //默认
//                "LTAIQPjOO5t9H9Oy",         //您的Access Key ID
//                "UN8gaJ6nLypQpLqikOfXIFpZfLfY4C");    //您的Access Key Secret
//        profile.getHttpClientConfig().setIgnoreSSLCerts(true);
//        IAcsClient client = new DefaultAcsClient(profile);
//        String biz = "postmanVerify"; //您在控制台上创建的、采用RPBasic认证方案的认证场景标识, 创建方法：https://help.aliyun.com/document_detail/59975.html

        if (ValidateUtil.isNull(biz) || null == client) {
            System.out.println("实名认证，系统配置不正确，请联系管理员3");
            return null;
        }

        //        //7. 服务端获取认证资料
//        //GetMaterials接口文档：https://help.aliyun.com/document_detail/57641.html
        GetMaterialsRequest getMaterialsRequest = new GetMaterialsRequest();
        getMaterialsRequest.setBiz(biz);
        getMaterialsRequest.setTicketId(ticketId);
        try {
            GetMaterialsResponse response = client.getAcsResponse(getMaterialsRequest);
            //后续业务处理
            if (null != response && null != response.getData()) {
//                System.out.println(response.getData().getSex());
//                System.out.println(response.getData().getAddress());
//                System.out.println(response.getData().getEthnicGroup());
//                System.out.println(response.getData().getFacePic());
//                System.out.println(response.getData().getIdCardBackPic());
//                System.out.println(response.getData().getIdCardFrontPic());
//                System.out.println(response.getData().getIdCardExpiry());
//                System.out.println(response.getData().getIdCardStartDate());
//                System.out.println(response.getData().getIdCardType());
//                System.out.println(response.getData().getIdentificationNumber());
//                System.out.println(response.getData().getName());

                return response.getData();
            }

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static void main(String[] args) {
//        System.out.println(getVerifyToken());
//        System.out.println(getVerifyStatus("c79b9d7e-35d4-4f99-a1ce-a344aaf3138f"));
        getVerifyInfo("c79b9d7e-35d4-4f99-a1ce-a344aaf3138f");
    }
}
